<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"favourite-s.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="最近在做一个视频播放的项目，所以遇到了常见的视频全屏的问题。在网络上可以查到很多关于视频旋转方面的资料。具体不再赘述。这里做一个总结。直接进入解决方案。">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS中视频旋转方案一">
<meta property="og:url" content="http://favourite-s.gitee.io/2020/08/20/iOS%E4%B8%AD%E8%A7%86%E9%A2%91%E6%97%8B%E8%BD%AC%E6%96%B9%E6%A1%88%E4%B8%80/index.html">
<meta property="og:site_name" content="左手边是幸福">
<meta property="og:description" content="最近在做一个视频播放的项目，所以遇到了常见的视频全屏的问题。在网络上可以查到很多关于视频旋转方面的资料。具体不再赘述。这里做一个总结。直接进入解决方案。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-20T02:20:03.000Z">
<meta property="article:modified_time" content="2020-08-20T03:55:16.409Z">
<meta property="article:author" content="favourite_s">
<meta property="article:tag" content="腾讯视频">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://favourite-s.gitee.io/2020/08/20/iOS%E4%B8%AD%E8%A7%86%E9%A2%91%E6%97%8B%E8%BD%AC%E6%96%B9%E6%A1%88%E4%B8%80/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>iOS中视频旋转方案一 | 左手边是幸福</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="左手边是幸福" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">左手边是幸福</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">知识总结</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://favourite-s.gitee.io/2020/08/20/iOS%E4%B8%AD%E8%A7%86%E9%A2%91%E6%97%8B%E8%BD%AC%E6%96%B9%E6%A1%88%E4%B8%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="favourite_s">
      <meta itemprop="description" content="只要胆子大，底牌全是炸">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="左手边是幸福">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS中视频旋转方案一
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-08-20 10:20:03 / 修改时间：11:55:16" itemprop="dateCreated datePublished" datetime="2020-08-20T10:20:03+08:00">2020-08-20</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>最近在做一个视频播放的项目，所以遇到了常见的视频全屏的问题。在网络上可以查到很多关于视频旋转方面的资料。具体不再赘述。这里做一个总结。直接进入解决方案。</p>
<a id="more"></a>

<h2 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h2><p>在项目中，我采用的是<code>Swift</code>语言，在我这个项目中。不同的控制器中导航栏的颜色、背景图片、样式等都不一致。可以参考另一篇章节介绍。采用了自定义导航栏实现（自定义view的方式）。一些基本概念和介绍可以看看掘金的这篇文章<a target="_blank" rel="noopener" href="https://juejin.im/post/6844903907978641422">iOS屏幕旋转—玩起来</a>讲的很详细了。</p>
<h3 id="一、AppDelegate"><a href="#一、AppDelegate" class="headerlink" title="一、AppDelegate"></a>一、AppDelegate</h3><p>在做一些配置项目的操作，运行之前可以先<code>clean</code>清一下缓存和删除已安装的<code>APP</code>。</p>
<h4 id="1、设置旋转权限"><a href="#1、设置旋转权限" class="headerlink" title="1、设置旋转权限"></a>1、设置旋转权限</h4><p>这里建议直接在<code>AppDelegate</code>中确定</p>
<ol>
<li>自定义<code>window</code></li>
</ol>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> window: <span class="type">UIWindow?</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">application</span><span class="params">(<span class="number">_</span> application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: <span class="keyword">Any</span>]?)</span></span> -&gt; <span class="type">Bool</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// Override point for customization after application launch.</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">self</span>.configJPush(launchOptions: launchOptions)</span><br><span class="line">    <span class="keyword">self</span>.configMob(application: application, options: launchOptions ?? [:])</span><br><span class="line">    </span><br><span class="line">    window = <span class="type">UIWindow</span>(frame: <span class="type">UIScreen</span>.main.bounds)</span><br><span class="line">    <span class="keyword">if</span> <span class="type">AuthManager</span>.shared.token == <span class="literal">nil</span> &#123;</span><br><span class="line">        window!.rootViewController = <span class="type">NBBaseNavigationController</span>(rootViewController: <span class="type">LoginViewController</span>())</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        window!.rootViewController = <span class="type">UTabBarController</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    window!.backgroundColor = .white</span><br><span class="line">    window!.makeKeyAndVisible()</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">self</span>.configNotification()</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/// 键盘处理</span></span><br><span class="line">    <span class="type">IQKeyboardManager</span>.shared.enable = <span class="literal">true</span></span><br><span class="line">    <span class="type">IQKeyboardManager</span>.shared.shouldShowToolbarPlaceholder = <span class="literal">true</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>实现代理方法(这一步很重要，由单页面自己控制旋转)</li>
</ol>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">application</span><span class="params">(<span class="number">_</span> application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)</span></span> -&gt; <span class="type">UIInterfaceOrientationMask</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>.window?.rootViewController?.supportedInterfaceOrientations ?? .portrait</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="二、基类控制器"><a href="#二、基类控制器" class="headerlink" title="二、基类控制器"></a>二、基类控制器</h3><p>在自定义的基类控制器中设定旋转凡是等。这里的基类控制器包含我们常用的<code>UITabbarViewController</code>，<code>UINavigationBarController</code>， <code>UIViewController</code>。</p>
<p>我这里用了 <code>OC</code> 的部分代码，有时间的话可以换成 <code>swift</code> 的</p>
<ol>
<li>例如 <code>NBBaseTabbarViewController</code> 继承自 <code>UITabbarViewController</code>，实现如下方法</li>
</ol>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 是否自动旋转</span></span><br><span class="line"><span class="comment">// 返回选中的是否支持旋转</span></span><br><span class="line">-(<span class="built_in">BOOL</span>)shouldAutorotate &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>.selectedViewController.shouldAutorotate;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 支持哪些屏幕方向</span></span><br><span class="line">- (<span class="built_in">UIInterfaceOrientationMask</span>)supportedInterfaceOrientations &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="keyword">self</span>.selectedViewController supportedInterfaceOrientations];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认方向</span></span><br><span class="line">- (<span class="built_in">UIInterfaceOrientation</span>)preferredInterfaceOrientationForPresentation &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="keyword">self</span>.selectedViewController preferredInterfaceOrientationForPresentation];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>例如 <code>NBBaseNavigationController</code> 继承自 <code>UINavigationBarController</code>，也要实现如下的方法</li>
</ol>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 是否自动旋转</span></span><br><span class="line"><span class="comment">// topViewController是其最顶层的视图控制器，</span></span><br><span class="line">-(<span class="built_in">BOOL</span>)shouldAutorotate&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>.topViewController.shouldAutorotate;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 支持哪些屏幕方向</span></span><br><span class="line">- (<span class="built_in">UIInterfaceOrientationMask</span>)supportedInterfaceOrientations &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="keyword">self</span>.topViewController supportedInterfaceOrientations];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认方向</span></span><br><span class="line">- (<span class="built_in">UIInterfaceOrientation</span>)preferredInterfaceOrientationForPresentation &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="keyword">self</span>.topViewController preferredInterfaceOrientationForPresentation];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>例如 <code>NBBaseViewController</code> 继承自 <code>UIViewController</code>，也要实现如下的方法</li>
</ol>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 是否自动旋转:所有控制器默认不自动旋转，需要横屏的视图控制器中覆写此方法，返回YES</span></span><br><span class="line">- (<span class="built_in">BOOL</span>)shouldAutorotate &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">NO</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 支持哪些屏幕方向:只支持竖屏</span></span><br><span class="line">- (<span class="built_in">UIInterfaceOrientationMask</span>)supportedInterfaceOrientations &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">UIInterfaceOrientationMaskPortrait</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认方向:只支持正常竖屏</span></span><br><span class="line">- (<span class="built_in">UIInterfaceOrientation</span>)preferredInterfaceOrientationForPresentation &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">UIInterfaceOrientationPortrait</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>项目中所有其他的控制器都继承这个 <code>NBBaseViewController</code>，如果不继承也可以自己重写上面的三个方法。上面的写法是为了默认设置为垂直方向。</p>
<h3 id="三、要旋转的控制器中"><a href="#三、要旋转的控制器中" class="headerlink" title="三、要旋转的控制器中"></a>三、要旋转的控制器中</h3><ol>
<li>定义全局变量</li>
</ol>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 记录是否进入后台</span></span><br><span class="line"><span class="keyword">fileprivate</span> <span class="keyword">var</span> isEnterBackground: <span class="type">Bool</span> = <span class="literal">false</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>监听进入后台和进入前台的通知，并实现通知方法</li>
</ol>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">NotificationCenter</span>.<span class="keyword">default</span>.addObserver(<span class="keyword">self</span>, selector: #selector(didBecomeActive), name: <span class="type">UIApplication</span>.didBecomeActiveNotification, object: <span class="literal">nil</span>)</span><br><span class="line"></span><br><span class="line"><span class="type">NotificationCenter</span>.<span class="keyword">default</span>.addObserver(<span class="keyword">self</span>, selector: #selector(didEnterBackground), name: <span class="type">UIApplication</span>.didEnterBackgroundNotification, object: <span class="literal">nil</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 进入后台</span></span><br><span class="line"><span class="meta">@objc</span> <span class="function"><span class="keyword">func</span> <span class="title">didBecomeActive</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="keyword">self</span>.isEnterBackground = <span class="literal">false</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 进入</span></span><br><span class="line"><span class="meta">@objc</span> <span class="function"><span class="keyword">func</span> <span class="title">didEnterBackground</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="keyword">self</span>.isEnterBackground = <span class="literal">true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>设定当前控制器支持旋转，和支持哪些旋转方向</li>
</ol>
<p>这个解决办法是参考<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/3c6a2a0294cc">iOS 关于全屏界面进入后台再打开变竖屏问题</a>这一篇文章的</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">VideoViewController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">override</span> <span class="keyword">var</span> shouldAutorotate: <span class="type">Bool</span> &#123;</span><br><span class="line">        <span class="comment">// 这个判断很重要，进入后台恢复状态使用（上面定义的变量就是为了这个地方）</span></span><br><span class="line">        <span class="comment">// 设备进入后台后，设备的 orientation 与你 app 中的 orientation 不一致的原因</span></span><br><span class="line">        <span class="comment">// 进入后台前，你的app是横向的，进入后台后，手机界面是竖向的</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">self</span>.isEnterBackground &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">override</span> <span class="keyword">var</span> supportedInterfaceOrientations: <span class="type">UIInterfaceOrientationMask</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> .allButUpsideDown</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>实现旋转过程中样式的改变</li>
</ol>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewWillTransition</span><span class="params">(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)</span></span> &#123;</span><br><span class="line">    <span class="keyword">super</span>.viewWillTransition(to: size, with: coordinator)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> orientation = <span class="type">UIDevice</span>.current.orientation</span><br><span class="line">    <span class="comment">//        let maxValue = max(UIScreen.fs.screenWidth(), UIScreen.fs.screenWidth())</span></span><br><span class="line">    <span class="comment">//        let minValue = min(UIScreen.fs.screenHeight(), UIScreen.fs.screenWidth())</span></span><br><span class="line">    <span class="comment">//        let width = orientation.isLandscape ? maxValue :minValue</span></span><br><span class="line"></span><br><span class="line">    <span class="type">UIView</span>.animate(withDuration: coordinator.transitionDuration, delay: <span class="number">0</span>, options: <span class="type">UIView</span>.<span class="type">AnimationOptions</span>.layoutSubviews, animations: &#123;</span><br><span class="line">        <span class="keyword">if</span> orientation.isLandscape &#123;</span><br><span class="line">            <span class="keyword">self</span>.isPortrait = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">self</span>.playerView.isPortrait = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">self</span>.playerContainerView.snp.remakeConstraints &#123; (make) <span class="keyword">in</span></span><br><span class="line">                make.edges.equalToSuperview()</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">self</span>.playerView.snp.remakeConstraints &#123; (make) <span class="keyword">in</span></span><br><span class="line">                make.edges.equalToSuperview()</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">self</span>.isPortrait = <span class="literal">true</span></span><br><span class="line">            <span class="keyword">self</span>.playerView.isPortrait = <span class="literal">true</span></span><br><span class="line">            <span class="keyword">let</span> height = kScreenW / <span class="number">16.0</span> * <span class="number">9.0</span></span><br><span class="line">            <span class="keyword">self</span>.playerContainerView.snp.remakeConstraints &#123; (make) <span class="keyword">in</span></span><br><span class="line">                make.top.equalToSuperview()</span><br><span class="line">                make.<span class="keyword">left</span>.<span class="keyword">right</span>.equalToSuperview()</span><br><span class="line">                make.height.equalTo(height+kStatusBarH)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">self</span>.playerView.snp.remakeConstraints(&#123; (make) <span class="keyword">in</span></span><br><span class="line">                make.<span class="keyword">left</span>.<span class="keyword">right</span>.bottom.equalToSuperview()</span><br><span class="line">                make.top.equalToSuperview().offset(kStatusBarH)</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">self</span>.view.setNeedsLayout()</span><br><span class="line">    &#125;, completion: <span class="literal">nil</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="四、设定屏幕的方向改变的方法"><a href="#四、设定屏幕的方向改变的方法" class="headerlink" title="四、设定屏幕的方向改变的方法"></a>四、设定屏幕的方向改变的方法</h3><p>可以放在控制器中，这里定了一个分类。在监听到全屏和窗口按钮点击的时候采用下面的凡是调用</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 全屏</span></span><br><span class="line"><span class="keyword">self</span>.fs.fullScreen()</span><br><span class="line"><span class="comment">// 退出全屏</span></span><br><span class="line"><span class="keyword">self</span>.fs.window()</span><br></pre></td></tr></table></figure>

<p><code>fs</code>的由来</p>
<p>新建一个 <code>FS.swift</code> 的文件，文件中的内容如下</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FS</span>&lt;<span class="title">Base</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> base: <span class="type">Base</span></span><br><span class="line">    <span class="keyword">init</span>(<span class="number">_</span> base: <span class="type">Base</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.base = base</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>新建一个 <code>FSCompatible.swift</code> 的文件，内容如下</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">protocol</span> <span class="title">FSCompatible</span> </span>&#123; &#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">FSCompatible</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">var</span> fs: <span class="type">FS</span>&lt;<span class="type">Self</span>&gt;.<span class="type">Type</span> &#123;</span><br><span class="line">        <span class="keyword">get</span> &#123; <span class="type">FS</span>&lt;<span class="type">Self</span>&gt;.<span class="keyword">self</span> &#125;</span><br><span class="line">        <span class="keyword">set</span> &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> fs: <span class="type">FS</span>&lt;<span class="type">Self</span>&gt; &#123;</span><br><span class="line">        <span class="keyword">get</span> &#123; <span class="type">FS</span>(<span class="keyword">self</span>) &#125;</span><br><span class="line">        <span class="keyword">set</span> &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> UIKit</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> class Foundation.NSObject</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">NSObject</span>: <span class="title">FSCompatible</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<p>这样就可以扩展自己的方法，可以防止<code>extension</code>冲突。例如著名的 <code>kingfisher</code> 也是采用这种方式<br>下面是全屏的方法</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">FS</span> <span class="title">where</span> <span class="title">Base</span>: <span class="title">UIViewController</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">fullScreen</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">let</span> orientationRawValue = <span class="type">UIInterfaceOrientation</span>.landscapeRight.rawValue</span><br><span class="line">        <span class="type">UIDevice</span>.current.setValue(orientationRawValue, forKey: <span class="string">&quot;orientation&quot;</span>)</span><br><span class="line">        base.setNeedsStatusBarAppearanceUpdate()</span><br><span class="line">        <span class="type">UIViewController</span>.attemptRotationToDeviceOrientation()</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         let orientation = UIDevice.current.orientation</span></span><br><span class="line"><span class="comment">         if orientation.isLandscape &#123;</span></span><br><span class="line"><span class="comment">             UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: &quot;orientation&quot;)</span></span><br><span class="line"><span class="comment">         &#125; else &#123;</span></span><br><span class="line"><span class="comment">             let orientationRawValue = UIInterfaceOrientation.landscapeRight.rawValue</span></span><br><span class="line"><span class="comment">             UIDevice.current.setValue(orientationRawValue, forKey: &quot;orientation&quot;)</span></span><br><span class="line"><span class="comment">         &#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">         self.setNeedsStatusBarAppearanceUpdate()</span></span><br><span class="line"><span class="comment">         UIViewController.attemptRotationToDeviceOrientation()</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">window</span><span class="params">()</span></span> &#123;</span><br><span class="line"><span class="comment">//        let orientation = UIDevice.current.orientation</span></span><br><span class="line">        <span class="type">UIDevice</span>.current.setValue(<span class="type">UIInterfaceOrientation</span>.portrait.rawValue, forKey: <span class="string">&quot;orientation&quot;</span>)</span><br><span class="line">        base.setNeedsStatusBarAppearanceUpdate()</span><br><span class="line">        <span class="type">UIViewController</span>.attemptRotationToDeviceOrientation()</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         let orientation = UIDevice.current.orientation</span></span><br><span class="line"><span class="comment">         if orientation.isLandscape &#123;</span></span><br><span class="line"><span class="comment">             UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: &quot;orientation&quot;)</span></span><br><span class="line"><span class="comment">             self.setNeedsStatusBarAppearanceUpdate()</span></span><br><span class="line"><span class="comment">             UIViewController.attemptRotationToDeviceOrientation()</span></span><br><span class="line"><span class="comment">         &#125; else &#123;</span></span><br><span class="line"><span class="comment">             self.navigationController?.popViewController(animated: true)</span></span><br><span class="line"><span class="comment">         &#125;</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%85%BE%E8%AE%AF%E8%A7%86%E9%A2%91/" rel="tag"># 腾讯视频</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/08/08/Docker/" rel="prev" title="Docker">
      <i class="fa fa-chevron-left"></i> Docker
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%A3%E6%96%87"><span class="nav-text">正文</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E3%80%81AppDelegate"><span class="nav-text">一、AppDelegate</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81%E8%AE%BE%E7%BD%AE%E6%97%8B%E8%BD%AC%E6%9D%83%E9%99%90"><span class="nav-text">1、设置旋转权限</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%9F%BA%E7%B1%BB%E6%8E%A7%E5%88%B6%E5%99%A8"><span class="nav-text">二、基类控制器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E8%A6%81%E6%97%8B%E8%BD%AC%E7%9A%84%E6%8E%A7%E5%88%B6%E5%99%A8%E4%B8%AD"><span class="nav-text">三、要旋转的控制器中</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%AE%BE%E5%AE%9A%E5%B1%8F%E5%B9%95%E7%9A%84%E6%96%B9%E5%90%91%E6%94%B9%E5%8F%98%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">四、设定屏幕的方向改变的方法</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="favourite_s"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">favourite_s</p>
  <div class="site-description" itemprop="description">只要胆子大，底牌全是炸</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">favourite_s</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">18k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">16 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>


  <script defer src="/lib/three/three.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













  

  

</body>
</html>
